/* 
        key:cookie名
        val:cookie值
        n:n天后过期
        */
//存
function setCookie(key, val, n) {
  let oDate = new Date();
  oDate.setDate(oDate.getDate() + n);
  document.cookie = `${key}=${val};expires=${oDate};path=/`;
}
// setCookie("username", "张三", 7);

//取
function getCookie(key) {
  let str = document.cookie;
  let arr = str.split("; ");
  for (let i = 0; i < arr.length; i++) {
    let arr1 = arr[i].split("=");
    if (arr1[0] === key) {
      return arr1[1];
    }
  }
  // getCookie("name")

  /* let val = "";
        arr.forEach((item) => {
          let arr1 = item.split("=");
          if (arr1[0] === key) {
            val = arr1[1];
          }
        });
        return val; */
}

//删
function removeCookie(key) {
  setCookie(key, 1, -1);
}
